package com.paishen.peiniwan;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.paishen.common.util.BaseUtil;
import com.paishen.common.util.LogUtil;
import com.paishen.common.util.SpfsUtil;

import java.util.ArrayList;
import java.util.List;


public class SplashActivity extends BaseActivity {

    private int[] coverIds = new int[]{R.mipmap.splash_001, R.mipmap.splash_002, R.mipmap.splash_003, R.mipmap.splash_004,};
    private List<View> splashList = new ArrayList<>();
    private List<ImageView> dotList = new ArrayList<>();
    private ViewPager viewPager;
    private ViewGroup layStepLight, layStepNormal;
    private Button btnEnter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isSupportTouchBack = false;
        setContentView(R.layout.activity_splash);

        BaseUtil.setStatusBarDarkMode( true,this);

        viewPager = (ViewPager) findViewById(R.id.view_pager);
        layStepLight = (ViewGroup) findViewById(R.id.lay_step_light);
        layStepNormal = (ViewGroup) findViewById(R.id.lay_step_normal);

        LinearLayout.LayoutParams lpDot1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lpDot1.setMargins(0, 0, BaseUtil.dip2px(8), 0);
        LinearLayout.LayoutParams lpDot2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        splashList.clear();
        for (int i = 0; i < coverIds.length; i++) {
            int cover = coverIds[i];
            ImageView imgCover = new ImageView(this);
            imgCover.setScaleType(ImageView.ScaleType.FIT_XY);
            imgCover.setImageResource(cover);
            splashList.add(imgCover);

            ImageView imgDotNormal = new ImageView(this);
            imgDotNormal.setImageResource(R.mipmap.splash_000_step_normal);
            imgDotNormal.setLayoutParams(i < coverIds.length - 1 ? lpDot1 : lpDot2);
            layStepNormal.addView(imgDotNormal);

            ImageView imgDotLight = new ImageView(this);
            imgDotLight.setImageResource(R.mipmap.splash_000_step_light);
            imgDotLight.setLayoutParams(i < coverIds.length - 1 ? lpDot1 : lpDot2);
            imgDotLight.setAlpha(i > 0 ? 0f : 1f);
            layStepLight.addView(imgDotLight);
            dotList.add(imgDotLight);
        }

        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return splashList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(splashList.get(position));
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(splashList.get(position));
                return splashList.get(position);
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            private int currPosition;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                currPosition = position;

                View curr = dotList.get(currPosition);
                curr.setAlpha(1 - positionOffset);
                if (currPosition + 1 < dotList.size()) {
                    View next = dotList.get(currPosition + 1);
                    next.setAlpha(positionOffset);

                    if (currPosition + 1 >= dotList.size() - 1) {
                        btnEnter.setAlpha(positionOffset);
                    }
                }
            }

            @Override
            public void onPageSelected(int position) {
                LogUtil.t("SplashActivity onPageSelected position:" + position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                LogUtil.d("SplashActivity onPageScrollStateChanged state:" + state);

                if(state == ViewPager.SCROLL_STATE_IDLE && currPosition >=  splashList.size() - 1) {
                    btnEnter.setEnabled(true);
                    btnEnter.setAlpha(1);
                } else {
                    btnEnter.setEnabled(false);
                }

            }
        });

        btnEnter =(Button) findViewById(R.id.btn_enter);
        btnEnter.setEnabled(false);
        btnEnter.setAlpha(0f);
        btnEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SpfsUtil.write(C.sp.SETTINGS_SPLASH_VERSION, BaseUtil.getAppVersionCode());
                Intent intent = new Intent(SplashActivity.this, LauncherActivity.class);
                startActivity(intent);
                finish();
            }
        });

        View viewStep = findViewById(R.id.lay_step);
        viewStep.setAlpha(0f);
        viewStep.animate().alpha(1).setDuration(3000).start();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.activity_fade_in, R.anim.activity_scale_exit);
    }
}
